//
//  ALPS_TSortingRadioViewController.m
//  Alps
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_TSortingRadioViewController.h"
#import "ALPS_TComprehensiveTCell.h"
#import "WSSNetTool.h"
#import "ALPS_TRadioModel.h"
#import "ALPS_PlayerMainViewController.h"
@interface ALPS_TSortingRadioViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
//加载页码
@property (nonatomic, assign) NSInteger page;
@end

@implementation ALPS_TSortingRadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    self.page = 1;
    [self getView];
    if (self.type == 2) {
        [self getRecommendedData];
    }
    else{
    [self getData:self.value page:1];
    }
    
    [self setLeftBackButton];
    self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
}


-(void)getView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, ScreeHeight - 64) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleGray;
    [self.view addSubview:_tableView];
    
    [self.tableView registerClass:[ALPS_TComprehensiveTCell class] forCellReuseIdentifier:@"ALPS_TComprehensiveTCell"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreshData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadingData];
    }];
    
}


//15个分类电台的数据请求
-(void)getData:(NSInteger)num page:(NSInteger)page
{
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://live.ximalaya.com/live-web/v2/radio/category?categoryId=%ld&device=iPhone&pageNum=%ld&pageSize=30", num, page] body:nil headers:nil response:0 success:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSArray *array = [dic objectForKey:@"data"];
        for (NSDictionary *dict in array) {
            ALPS_TRadioModel *model = [[ALPS_TRadioModel alloc] initWithDic:dict];
            [self.dataSource addObject:model];
        }
        
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

//推荐页的网络请求
-(void)getRecommendedData
{
    [WSSNetTool GetNetByURL:@"http://live.ximalaya.com/live-web/v1/radio/local?device=iPhone&pageNum=1&pageSize=30" body:nil headers:nil response:0 success:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSArray *array = [dic objectForKey:@"data"];
        for (NSDictionary *dict in array) {
            ALPS_TRadioModel *model = [[ALPS_TRadioModel alloc] initWithDic:dict];
            [self.dataSource addObject:model];
        }
        
        
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}


//刷新
-(void)refreshData
{
    if (self.type == 2) {
        [self.dataSource removeAllObjects];
        [self.tableView reloadData];
        [self getRecommendedData];
    }
    else
    {
    [self.dataSource removeAllObjects];
    [self.tableView reloadData];
    [self getData:self.value page:1];
    self.page = 1;
    }
}

//加载
-(void)loadingData
{
    self.page++;
    [self getData:self.value page:self.page];
    
}





-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_TComprehensiveTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ALPS_TComprehensiveTCell"];
    cell.model = [self.dataSource objectAtIndex:indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_PlayerMainViewController *pmVC = [[ALPS_PlayerMainViewController alloc]init];
    ALPS_TRadioModel *model = [self.dataSource objectAtIndex:indexPath.row];
    NSDictionary *dic = model.playUrl;
    pmVC.radioName = model.name;
    pmVC.radioURL = [dic objectForKey:@"aac64"];
    pmVC.isRedioYesOrNo = YES;
    pmVC.pictureURL = model.coverLarge;
    [self.navigationController pushViewController:pmVC animated:YES];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
